package com.sxs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxs.commonutils.R;
import com.sxs.entity.EduTeacher;
import com.sxs.entity.vo.TeacherQuery;
import com.sxs.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 讲师(EduTeacher)表控制层
 *
 * @author song
 * @Api 作用是在使用swagger时给controller添加中文提示（可不写）
 * @since 2021-08-07 19:35:36
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduService/eduTeacher")
@CrossOrigin  //解决跨域问题
public class EduTeacherController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有数据
     *
     * @return 所有数据
     * @ApiParam 作用是使用swagger时给参数添加中文提示（可不写）
     * @ApiOperation 作用是使用swagger时给方法添加中文提示（可不写）
     * @ApiOperation (value = “ 接口说明 ”, httpMethod = “ 接口请求方式 ”, response = “ 接口返回参数类型 ”, notes = “ 接口发布说明 ”
     */
    @ApiOperation(value = "所有讲师数据")
    @GetMapping("findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("list", list);
    }

    /**
     * 分页查询所有数据
     *
     * @param current 当前页
     * @param limit   每页数量
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询所有讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R selectAllByPage(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页数量", required = true)
            @PathVariable Long limit) {
        Page<EduTeacher> page = new Page<>(current, limit);
        IPage<EduTeacher> page1 = eduTeacherService.page(page, null);
        return R.ok().data("page1", page1);
    }

    /**
     * 条件查询数据以及分页
     *
     * @param current 当前页
     * @param limit   每页数量
     * @return 所有数据
     */
    @ApiOperation(value = "分页以及条件查询所有讲师")
    @PostMapping("pageTeacherByCondition/{current}/{limit}")
    public R selectAllByPageByCondition(
            @ApiParam(name = "current", value = "当前页", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页数量", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "条件对象", required = true)
            @RequestBody(required = false) TeacherQuery teacherQuery) {
        System.out.println(current);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        Page<EduTeacher> page = new Page<>(current, limit);
        IPage<EduTeacher> page1 = eduTeacherService.page(page, queryWrapper);
        return R.ok().data("page1", page1);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "查询单个讲师")
    @GetMapping("{id}")
    public R selectOne(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher", byId);
    }

    /**
     * 新增数据
     *
     * @param eduTeacher 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R insert(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? R.ok() : R.error();
    }

    /**
     * 修改数据
     *
     * @param eduTeacher 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "更新讲师")
    @PostMapping("updateTeacher")
    public R update(@RequestBody EduTeacher eduTeacher) {
        boolean b = eduTeacherService.updateById(eduTeacher);
        return b ? R.ok() : R.error();
    }

    /**
     * 逻辑删除数据
     *
     * @param id 主键结合
     * @return 逻辑删除结果
     * id通过路径传递
     */
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R deleteTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        return flag ? R.ok() : R.error();

    }
}
